<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<jsp:useBean id="db" scope="session" class="com.jipl.Masters.DatabaseBean"/>
<jsp:setProperty name="db" property="*"/>
<%@ page import="java.io.* , java.util.*, java.sql.*,  com.jipl.login.*"%>
<%
String b="select distinct(itype) from itypes";
db.connect();
ResultSet rsb=db.execSQL(b);	
if (rsb.next()==true)
{	%>
<html xmlns="http://www.w3.org/1999/xhtml">
    <!-- DW6 -->
    <head>
        <!-- Copyright 2005 Macromedia, Inc. All rights reserved. -->
        <title>Edit Item type</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link rel="stylesheet" href="images/mm_health_nutr.css" type="text/css" />
        <script language="JavaScript" type="text/javascript">
            //--------------- LOCALIZEABLE GLOBALS ---------------
            var d=new Date();
            var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
            //Ensure correct for language. English is "January 1, 2004"
            var TODAY = monthname[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
            //---------------   END LOCALIZEABLE   ---------------
        </script>
        <script language="JavaScript" type="text/javascript" src="Scripts/ItemMasterValidations.js">


        </script>
        <style type="text/css">
            <!--
            .style1 {
                color: #F4FFE4;
                font-weight: bold;
                font-size: 12px;
            }
            .style2 {
                font-size: 14px;
                font-weight: bold;
            }
            -->
        </style>
        <link href="../../images/mm_health_nutr.css" rel="stylesheet" type="text/css" />
        <style type="text/css">
            <!--
            a {
                font-size: 12px;
                font-weight: bold;
            }
            a:link {
                text-decoration: none;
            }
            a:visited {
                text-decoration: none;
            }
            a:hover {
                text-decoration: none;
            }
            a:active {
                text-decoration: none;
            }
            -->
        </style></head>
    <body bgcolor="#5C743D">
        <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">


            <tr>
                <td  align="center" valign="top" background="../../images/flash_body1.png"><p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <p>&nbsp;</p>
                    <table width="610" border="0" align="center" cellpadding="2" cellspacing="0">
                        <tr>
                            <td height="180" align="center" valign="top" class="pageName"><p><br />
                                </p>
                                <form id="form1" name="form1" method="post" action="deleteType.jsp" onsubmit="return validateDeleteType();">
                                    <table width="313" align="center" style="border-right: #FFFFFF 1px solid;
                                           border-top: #FFFFFF 1px solid; border-left: #FFFFFF 1px solid;  border-bottom: #FFFFFF 1px solid; font-size: 9pt; color: #000000; font-family: Arial;">
                                        <tr>
                                            <td height="32" colspan="2" align="center" valign="middle" background="../../images/flash_header1.png" bgcolor="#3E7550"><span class="style2">Delete a  Type </span></td>
                                        </tr>
                                        <tr>
                                            <td width="120" align="center" valign="middle" background="../../images/flash_body1.png"><span class="style1">Item Type </span></td>
                                            <td width="181" align="center" valign="middle" background="../../images/flash_body1.png"><select name="select">
                                                    <option> </option>
                                                    <%@ page import="java.io.* , java.util.*, java.sql.*"%>
                                                    <%
                                                                            String q="select distinct(itype) from itypes";
                                                                            db.connect();
                                                                            ResultSet rs=db.execSQL(q);
                                                                            while (rs.next()==true)
                                                                            {
                                                    %>
                                                    <option><%=rs.getString(1)%></option>
                                                    <%}%>
                                                </select></td>
                                        </tr>
                                        <tr>
                                            <td colspan="2" align="center" valign="middle" background="../../images/flash_body1.png"><input name="delete" type="submit" class="subHeader" id="delete" value="Delete" /></td>
                                        </tr>
                                    </table>
                                </form>
                                <% if (request.getParameter("delete")!=null)
                    {
                    db.connect();
                                    int s=0;
                                    String itype=request.getParameter("select");
                                    String get_item_id="select ino from itypes where itype='"+itype+"'";
                                            System.out.println(get_item_id+"-----------");
                                            ResultSet get_it_id=db.execSQL(get_item_id);
                                            int gid=0;
                                            if (get_it_id.next()==true)
                                                            gid=get_it_id.getInt(1);
                                    String ig="select igname,igroupid from igroup where itname="+gid;
                                    System.out.print(ig);
                                    ResultSet group=db.execSQL(ig);
                                    Vector g=new Vector();
                                    Vector id=new Vector();
                                    while (group.next()==true)
                                    {
                                            g.add(group.getString(1));
                                            id.add(group.getString(2));
                                    }
                                    if (group.next()==false)
                                    {
                                    String s12="delete from itypes where itype='"+itype+"'";
                                    s=db.updateSQL(s12);
                                    }
                                    for (int i=0;i<id.size();i++)
                                    {
                                    String checkItems="select itemname from items where igroupid='"+id.elementAt(i)+"'";
                                    ResultSet rset=db.execSQL(checkItems);
                                    if (rset.next()==false)
                                    {
                                    System.out.print("false");
                                    String s12="delete from itypes where itype='"+itype+"'";
                                    s=db.updateSQL(s12);
                                    }
                                    while (rset.next()==true)
                                    {
                                    String check="select * from estimateitems where iname='"+rset.getString(1)+"'";
                                    System.out.print(check);
                                    ResultSet r1=db.execSQL(check);
		
                                    String check1="select * from purchase where iname='"+rset.getString(1)+"'";
                                    System.out.print(check1);
                                    ResultSet r2=db.execSQL(check1);
		
                                    String check2="select * from allocation where itemname='"+rset.getString(1)+"'";
                                    ResultSet r3=db.execSQL(check2);
                                    System.out.print(check2);
		
                                    String check3="select * from acceptance where rem='"+rset.getString(1)+"'";
                                    ResultSet r4=db.execSQL(check3);
                                    System.out.print(check3);
		
                                    String check4="select * from workentry where itemname='"+rset.getString(1)+"'";
                                    ResultSet r5=db.execSQL(check4);
                                    System.out.print(check4);
		
		
                                            if (r1.next()==false && r2.next()==false && r3.next()==false && r4.next()==false && r5.next()==false)
                                            {
                                                    String items="delete from items where igroupid='"+id.elementAt(i)+"'";
                                                    System.out.print(items);
                                                    int s2=db.updateSQL(items);
		
                                                    for (int j=0;j<g.size();j++)
                                                    {
                                                    String group1="delete from igroup where igname='"+g.elementAt(j)+"'";
                                                    System.out.print(group1);
                                                    int s3=db.updateSQL(group1);
                                                    }
                                                    String s12="delete from itypes where itype='"+itype+"'";
                                                    s=db.updateSQL(s12);
                                            }else {out.print("<B>Record Not Removed1!...<br /><a href='itemMaster.jsp'>Please Try Again!......</a></b>");}
                                    }
		
		
                                    }
		
                                    if (s>0)
                                    {
                                    out.println("<script language='javascript'>alert('Record Removed!..........');location='deleteType.jsp'</script>");
                                    }else if (s<=0) {
                                    out.println("<script language='javascript'>alert('Record Not Removed!..........');location='deleteType.jsp'</script>");
                                    }
                    }
                                %>
                                <%}else if (rsb.next()==false)
                      out.println("No Records...Plz. insert Records...<br></h2><a href='itemMaster.jsp'>Go To Item Master</a></strong>");
                                %></td>
                        </tr>
                    </table></td>
            </tr>
        </table>
    </body>
</html>
